using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

   public  class CLSPRVMAT : ObservableObject
    {

        MDOgeneral mod = new MDOgeneral();


        public List<CLSPRVMAT> ListarMatPro() 
        {
            try
            {
                string query = "select CODMAT_PXM,(select NOMPRV_PRV from MAEPRVP where CODPRV_PRV=CODPRV_PXM ) as NOMPRV ,CODPRV_PXM,COSTO_PXM, CONSEC_PXM from PRVMAT where CODMAT_PXM='" + CODMAT_PXM + "'";
                DataTable val = mod.consulta(query);
                List<CLSPRVMAT> MatPro = new List<CLSPRVMAT>();
        
                for (int i = 0; i <= val.Rows.Count - 1; i++) {
                    MatPro.Add(new CLSPRVMAT {
                        CODMAT_PXM=val.Rows[i]["CODMAT_PXM"].ToString(),
                        NOMPRV = val.Rows[i]["NOMPRV"].ToString(),
                        CODPRV_PXM=val.Rows[i]["CODPRV_PXM"].ToString(),
                        COSTO_PXM=int.Parse(val.Rows[i]["COSTO_PXM"].ToString()),
                        CONSEC_PXM = int.Parse(val.Rows[i]["CONSEC_PXM"].ToString()), 
                        EXISTE=true
                    });
                }
                return MatPro;
            }
            catch (Exception ex) { throw (ex); }
        }

        public int insertar() 
        {
            try 
            {
                string query = "insert into PRVMAT (CODMAT_PXM,CODPRV_PXM,COSTO_PXM, CONSEC_PXM) values ('" + CODMAT_PXM + "','" + CODPRV_PXM + "'," + COSTO_PXM + ", " + CONSEC_PXM + ")";
                return mod.comando(query);
            }
            catch(Exception ex){ throw(ex); }
        }   

        public int actualizar()
        {
            try
            {
                string query = "update PRVMAT set COSTO_PXM="+COSTO_PXM+", CONSEC_PXM = " + CONSEC_PXM + " where CODMAT_PXM='"+CODMAT_PXM+"' and CODPRV_PXM='"+CODPRV_PXM+"'";
                return mod.comando(query);
            }
            catch (Exception ex)   { throw (ex);  }
        }

       public int eliminar()
        {
            try
            {
                string query = "delete from PRVMAT where CODMAT_PXM='"+CODMAT_PXM+"' and CODPRV_PXM='"+CODPRV_PXM+"'";
                return mod.comando(query);
            }
            catch (Exception ex) { throw (ex); }
        }

        public int eliProMat()
        {
            try
            {
                string query = "delete from PRVMAT where CODMAT_PXM='" + CODMAT_PXM + "'";
                return mod.comando(query);
            }
            catch (Exception ex) { throw (ex); }
        }

        public Boolean existe()
        {
            try
            {
                string query = "select count(*) from PRVMAT where CODMAT_PXM='" + CODMAT_PXM + "' and CODPRV_PXM='" + CODPRV_PXM + "'";
                DataTable val = mod.consulta(query);
                return val.Rows[0][0].ToString().Equals("0")?false:true;
            }
            catch (Exception ex) { throw (ex); }
        }

        public Boolean guardarPrv(String StrCodigoMaterial, List<CLSPRVMAT> LisProveedor)
        {
            Boolean sw = true;
            MDOgeneral MdoGeneral = new MDOgeneral();//FJRF - Instanciamos todas las funciones que pueden existir en Mdogenral

            MdoGeneral.comando("DELETE FROM PRVMAT WHERE CODMAT_PXM = '" + StrCodigoMaterial + "' ");//FJRF - Eliminamos todos los proveedores que pueda tener ese material para grabarlos nuevamente independientemente de que el proveedor exista o sea nuevo
            String StrCodigoProveedor = "";//FJRF - Variable que contendra el codigo del proveedor
            try
            {
                for (int i = 0; i <= LisProveedor.Count - 1; i++)
                {
                    if (LisProveedor[i].CODPRV_PXM!=null)
                    {
                        if (LisProveedor[i].CAMBIO &&   LisProveedor[i].CODPRV_PXM!="")
                        {
                            StrCodigoProveedor = LisProveedor[i].CODPRV_PXM;//FJRF - Extraemos el codigo del proveedor

                            if (LisProveedor[i].ELIMINAR == true && LisProveedor[i].EXISTE == true)
                            {
                                if (LisProveedor[i].eliminar() <= 0)
                                {
                                    sw = false;
                                }
                            }
                            else
                            {
                                if (LisProveedor[i].ELIMINAR == false)
                                {
                                    if (LisProveedor[i].existe() == true)
                                    {
                                        if (LisProveedor[i].actualizar() <= 0)
                                        {
                                            sw = false;
                                        }
                                    }
                                    else
                                    {
                                        if (LisProveedor[i].insertar() <= 0)
                                        {
                                            sw = false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { throw(ex); }
            return sw;
        }

        public Boolean lisExiste(object _lisSub, object _Sub)
        {
            try
            {
                List<CLSPRVMAT> lisSub = (List<CLSPRVMAT>)_lisSub;
                CLSPRVMAT Sub = (CLSPRVMAT)_Sub;
                if (Sub.CODPRV_PXM == null) { return false; }
                if (lisSub.FindAll(r => r.CODMAT_PXM.Equals(Sub.CODMAT_PXM) & r.CODPRV_PXM.Equals(Sub.CODPRV_PXM)).Count > 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex) { throw (ex); }
        }

        public Boolean exiMov()
        {
            try
            {

                //Tiene Pedidos
                string query = query = "select count(*) from DETPPRP where CODPRV_DPP='"+CODPRV_PXM+"' and CODMAT_DPP='" + CODMAT_PXM + "'";
                if (!mod.consulta(query).Rows[0][0].ToString().Equals("0"))
                {
                    return true;
                }
  
                return false;
            }
            catch (Exception ex) { throw (ex); }
        }


        #region  Propiedades 
    
            private string _CODMAT_PXM;
            public string CODMAT_PXM
            {
                get { return _CODMAT_PXM;}
                set { _CODMAT_PXM=value; RaisePropertyChangedEvent("CODMAT_PXM"); }
            }
            private string _CODPRV_PXM;
            public string CODPRV_PXM
            {
                get { return _CODPRV_PXM;}
                set 
                {
                    if (_CODPRV_PXM != value) { CAMBIO = true; }
                    _CODPRV_PXM=value;
                    RaisePropertyChangedEvent("CODPRV_PXM");
                }
            }
            private int _COSTO_PXM;
            public int COSTO_PXM
            {
                get { return _COSTO_PXM;}
                set 
                {
                    if (_COSTO_PXM != value) { CAMBIO = true; }
                    _COSTO_PXM = value;
                    RaisePropertyChangedEvent("COSTO_PXM");
                }
            }
            private int _CONSEC_PXM;
            public int CONSEC_PXM
            {
                get { return _CONSEC_PXM; }
                set
                {
                    if (_CONSEC_PXM != value) { CAMBIO = true; }
                    _CONSEC_PXM = value;
                    RaisePropertyChangedEvent("CONSEC_PXM");
                }
            }
            private string _NOMPRV;
            public string NOMPRV
            {
                get { return _NOMPRV; }
                set { _NOMPRV = value; RaisePropertyChangedEvent("NOMPRV"); }
            }
            private Boolean _CAMBIO=false;
            public Boolean CAMBIO
            {
                get { return _CAMBIO; }
                set { _CAMBIO = value; RaisePropertyChangedEvent("CAMBIO"); }
            }
            private Boolean _EXISTE=false;
            public Boolean EXISTE
            {
                get { return _EXISTE; }
                set { _EXISTE = value;  RaisePropertyChangedEvent("EXISTE"); }
            }
            private Boolean _ELIMINAR = false;
            public Boolean ELIMINAR
            {
                get { return _ELIMINAR; }
                set
                {
                    if (_ELIMINAR != value) { CAMBIO = true; }
                    _ELIMINAR = value;
                    RaisePropertyChangedEvent("ELIMINAR");
                }
            }      

        #endregion

      }
}
